package com.ruyuan.payment.pay.controller;

import com.ruyuan.payment.server.context.LoginMemberContext;
import com.ruyuan.payment.server.req.OrderInfoQueryReq;
import com.ruyuan.payment.server.req.OrderInfoAddReq;
import com.ruyuan.payment.server.resp.CommonResp;
import com.ruyuan.payment.server.resp.OrderInfoQueryResp;
import com.ruyuan.payment.server.resp.OrderInfoAddResp;
import com.ruyuan.payment.server.resp.PageResp;
import com.ruyuan.payment.server.service.OrderInfoService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

@RestController
@RequestMapping("/order-info")
public class OrderInfoController {

    @Resource
    private OrderInfoService orderInfoService;

    @PostMapping("/add")
    public CommonResp add(@RequestBody OrderInfoAddReq req) {
        OrderInfoAddResp resp = orderInfoService.add(req);
        CommonResp<OrderInfoAddResp> commonResp = new CommonResp<>();
        commonResp.setContent(resp);
        return commonResp;
    }

    @GetMapping("/query-list")
    public CommonResp queryList(@Valid OrderInfoQueryReq req) {
        req.setMemberId(LoginMemberContext.getId());
        CommonResp<PageResp<OrderInfoQueryResp>> commonResp = new CommonResp<>();
        PageResp<OrderInfoQueryResp> list = orderInfoService.queryList(req);
        commonResp.setContent(list);
        return commonResp;
    }

    @GetMapping("/cancel/{orderNo}")
    public CommonResp closeChannel(@PathVariable String orderNo) {
        CommonResp<String> commonResp = new CommonResp<>();
        String message = orderInfoService.cancel(orderNo);
        commonResp.setContent(message);
        return commonResp;
    }
}
